import cTitle from "components/title";
import diyBackGround from "components/diyBackGround";
import searchRecord from "./components/searchRecord.vue"
import { Toast } from 'vant';
import { scrollMixin } from 'utils/mixin';
import {bindDrainageScene} from "../../../fun/modules/drainage_scene";

export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      navInd: 0,
      searchKey: '',
      show: false,
      showIndex: '',
      datainfo: [],
      list: [],
      display_set: {},
      userLeave: [],
      // 筛选条件
      //是否推广员
      is_agent: "",
      //会员等级id
      level_id: {
        name: '',
        id: ''
      },
      //排序
      orderby: {
        name: '',
        id: ''
      },
      // 轨迹
      browse: {
        name: '',
        id: ''
      },
      //more
      isLoadMore: true,
      page: 1,
      total_page: 0,
    }
  },
  activated() {
    this.getData();
    this.init();
    this.getList();
  },
  computed: {
    setTitle() {
      if (this.showIndex == 0) {
        return '会员等级'
      } else if (this.showIndex == 1) {
        return '是否推广员'
      } else if (this.showIndex == 2) {
        return '排序'
      } else if (this.showIndex == 3) {
        return '轨迹'
      }
    },
    selectOption() {
      if (this.showIndex == 1) {
        return [
          {
            id: '',
            name: '是否为推广员'
          },
          {
            id: 0,
            name: '不是推广员'
          },
          {
            id: 1,
            name: '推广员'
          }
        ]
      } else if (this.showIndex == 2) {
        return [
          {
            id: "",
            name: '排序'
          },
          {
            id: 0,
            name: '注册时间倒序'
          },
          {
            id: 1,
            name: '注册时间正序'
          },
          {
            id: 2,
            name: '订单数量'
          },
          {
            id: 3,
            name: '订单总额'
          },
          {
            id: 4,
            name: '客户数量'
          },
          {
            id: 5,
            name: '客户订单总金额'
          }
        ]
      } else if (this.showIndex == 3) {
        return [
          {
            id: 0,
            name: '轨迹'
          },
          {
            id: 1,
            name: '一天之内'
          },
          {
            id: 2,
            name: '三天之内'
          },
          {
            id: 3,
            name: '七天之内'
          },
          {
            id: 4,
            name: '30天未访问过'
          }
        ]
      }
    },

  },
  methods: {
    toPlugin(name){
      console.log(name)
      if(name == 'material-center'){
        // materialCenter
        this.$router.push(this.fun.getUrl('materialCenter'));
      }else if(name == 'article'){
        // notice
        this.$router.push(this.fun.getUrl('notice'));
      }else if(name == 'video-share'){
        this.$router.push(this.fun.getUrl('VideoList'));
      }else if(name == "micro-communities"){
        this.$router.push(this.fun.getUrl('MicroHome'));
      }
    },
    goTrack() {
      this.$router.push(this.fun.getUrl('customerCenterTrack', { is_type: this.navInd }));
    },
    onConfirm(e) {
      console.log(this.showIndex, e);
      if (this.showIndex == 0) {
        this.level_id.id = e.id;
        this.level_id.name = e.level_name;
      } else if (this.showIndex == 1) {
        this.is_agent = e.id;
        console.log(this.is_agent)
      } else if (this.showIndex == 2) {
        this.orderby = e;
      } else {
        this.browse = e;
      }
      this.show = false;
      this.getList();
    },
    onCancel() {
      console.log(this.showIndex);
      this.show = false;
    },
    init() {
      // this.list = [];
      this.isLoadMore = true;
      this.page = 1;
      this.total_page = 0;
      // 初始化搜索条件
      this.keyword = '';
      this.is_agent = '';
      this.level_id = {
        name: '',
        id: ''
      };
      this.orderby = {
        name: '注册时间倒序',
        id: '0'
      };
      this.browse = {
        name: '',
        id: ''
      };
    },
    getList() {
      let json = {
        search: {
          level_type: this.navInd,
          keyword: this.searchKey,
        }
      }
       // 推广员
       json.search.is_agent = this.is_agent;
       // 排序
       json.search.orderby = this.orderby.id;
       // 会员等级
       if (this.level_id && this.level_id.id) {
         json.search.level_id = this.level_id.id
       }
       // 轨迹
       if (this.browse && this.browse.id) {
         json.search.browse = this.browse.id
       }
      $http
        .post("plugin.customer-center.frontend.controllers.index.get-customer-list", json, '')
        .then(response => {
          if (response.result === 1) {
            this.isLoadMore = true;
            this.total_page = response.data.customer_list.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.list = response.data.customer_list.data;
            this.display_set = response.data.display_set;
          } else {
            Toast(response.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        let json = {
          search: {
            level_type: this.navInd,
            keyword: this.searchKey,
          }
        }
        // 推广员
        json.search.is_agent = this.is_agent;
        // 排序
        json.search.orderby = this.orderby.id;
        // 会员等级
        if (this.level_id && this.level_id.id) {
          json.search.level_id = this.level_id.id
        }
        // 轨迹
        if (this.browse && this.browse.id) {
          json.search.browse = this.browse.id
        }
        this.page = this.page + 1;
        json.page = this.page;
        $http.post("plugin.customer-center.frontend.controllers.index.get-customer-list", json, '加载中').then(
          response => {
            let res = response.data;
            that.isLoadMore = true;
            if (response.result === 1) {
              var myData = res.customer_list.data;
              that.list = that.list.concat(myData); //数组拼接
            } else {
              that.page = that.page - 1;
              that.isLoadMore = false;
            }
          },
          function (response) {
            console.log('response', response);
            // error callback
          }
        );
      }
    },
    onCopy() {
      Toast({
        message: '复制成功',
        position: 'bottom',
        duration: 5000
      });
    },
    telCall(phone) {
      window.location.href = `tel://${phone}`;
    },
    setShowInd(key) {
      this.showIndex = key;
      this.show = true;
      if (key == 0 && this.userLeave.length == 0) {
        this.getUser();
      }
    },
    tapNav(ind) {
      this.navInd = ind;
      this.init();
      this.getList();
    },
    onClickRight() {
      this.show = false;
    },
    onClickLeft() {
      this.show = false;
    },
    getUser() {
      $http
        .get(
          "plugin.customer-center.frontend.controllers.index.get-level-list",
          {},
          "加载中"
        )
        .then(response => {
          if (response.result === 1) {
            let list = response.data.level_list;
            list = [{
              id: '',
              level_name: "请选择会员等级"
            }, ...list]
            this.userLeave = list;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getData() {
      $http
        .get(
          "plugin.customer-center.frontend.controllers.index.index",
          {},
          "加载中"
        )
        .then(response => {
          if (response.result === 1) {
            let isBindDrainageScene = bindDrainageScene({
              scene: 5,
              title: '客户中心'
            });
            if(!isBindDrainageScene) return;//爆客码绑定场景

            this.datainfo = response.data
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
  },
  components: { cTitle, diyBackGround, searchRecord }
}